package com.laozhao.dreamframe.sqlmap;

import java.util.Date;
import java.util.HashMap;

public class BaseInsert {
	/**
	 * sql语句
	 */
	private String command;
	/**
	 * update 内容字段
	 */
	private HashMap<String, Object> insertSet;


//	public BaseInsert() {
//		insertSet = new HashMap<String, Object>();
//	}
	
	public BaseInsert(String command, HashMap<String, Object> map) {
		this.command = command;
		insertSet =map;
	}


	/**
	 * 生成最终sql语句
	 */
	public String toSql() {
		if(insertSet == null || insertSet.isEmpty() ){
			return null;
		}

		command = command.replace("$columnset" , getInsertColumnString());
		command = command.replace("$valueset" , getInsertValueString());
		command = command.replaceAll("$sysdate", new Date().toString());
		
		
		return command;

	}



	public String getInsertColumnString() {
		StringBuffer sqlUpdate = new StringBuffer();
		
		for (String key : insertSet.keySet()) {
			sqlUpdate.append(key).append(" , ");
		}
		
		
		return sqlUpdate.substring(0, sqlUpdate.length() - 2);
	}
	
	public String getInsertValueString() {
		StringBuffer sqlUpdate = new StringBuffer();
		
		for (String key : insertSet.keySet()) {
			sqlUpdate.append("'").append(insertSet.get(key)).append("'").append(" , ");
		}
		
		
		return sqlUpdate.substring(0, sqlUpdate.length() - 2);
	}

	
//	String getFieldName(String field) {
//
//		if (field == null) {
//			throw new RuntimeException(field + " cannot be null");
//		}
//		return field.toUpperCase();
//	}
	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public HashMap<String, Object> getUpdateSet() {
		return insertSet;
	}

	public void setUpdateSet(HashMap<String, Object> updateSet) {
		this.insertSet = updateSet;
	}

}
